<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Map数据结构</title>
</head>
<body>
    <h1>Map数据结构</h1>
    <h2>Map类似于对象，但是键可以是任何类型</h2>

    <script>
        // Map的基本用法
        const map = new Map();
        const obj = {p: 'Hello'};
        
        map.set(obj, 'content');
        console.log(map.get(obj)); // "content"

        // 接受数组作为参数
        const map2 = new Map([
            ['name', '张三'],
            ['age', 18]
        ]);
        console.log(map2.get('name')); // "张三"

        // Map的方法
        console.log(map2.has('name')); // true
        console.log(map2.size); // 2
        map2.delete('name');
        map2.clear();

        // Map的遍历
        const map3 = new Map([
            ['F', 'no'],
            ['T', 'yes'],
        ]);

        for (let key of map3.keys()) {
            console.log(key);
        }

        for (let value of map3.values()) {
            console.log(value);
        }

        for (let [key, value] of map3.entries()) {
            console.log(key, value);
        }
    </script>
</body>
</html> 